using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SBPweb.Presentation.Windows.Forms.Controls;
using SBPweb.Localization;
using System.Text.RegularExpressions;

namespace SBPweb.Modules.ContactsManager.Controls
{
	public partial class RenameGroupControl : ModalDockingUserControl
	{
		#region Private members

		private ContactListControl mContactListControl;
		private string mGroupName;
		private Regex mGroupNameRegex = new Regex(@"^([\w\s0-9]([\w\-\s0-9])*)$");

		#endregion

		#region Constructor logic

		public RenameGroupControl(ContactListControl contactListControl, string groupName)
			:base(contactListControl.ParentModule)
		{
			this.Caption = LocalizationExtender.GetString("RenameGroupControl");
			if (this.Parent != null)
			{
				this.Parent.Resize += new EventHandler(Parent_Resize);
			}
			LocalizationExtender.OnInvalidatedControls += new EventHandler(LocalizationExtender_OnInvalidatedControls);
			mContactListControl = contactListControl;
			mGroupName = groupName;
			InitializeComponent();

			InitializeTextboxes();
		}

		private void InitializeTextboxes()
		{
			tbOldName.Text = tbNewName.Text = mGroupName;
		}

		#endregion

		private void Finalize()
		{
			this.Parent.Resize -= new EventHandler(Parent_Resize);
			LocalizationExtender.OnInvalidatedControls -= new EventHandler(LocalizationExtender_OnInvalidatedControls);
		}

		#region Event handlers

		private void LocalizationExtender_OnInvalidatedControls(object sender, EventArgs e)
		{
			this.Caption = LocalizationExtender.GetString("RenameGroupControl");
			Parent_Resize(sender, e);
		}

		private void Parent_Resize(object sender, EventArgs e)
		{
			this.Size = this.Parent.ClientSize;
			this.btnCancel.Location = new Point(this.Width - 5 - btnCancel.Width, this.Height - 5 - btnCancel.Height);
			this.btnSave.Location = new Point(5, this.Height - 5 - btnSave.Height);
			this.tbNewName.Width = this.tbOldName.Width = this.Width - this.tbOldName.Location.X;
		}

		private void tbNewName_TextChanged(object sender, System.EventArgs e)
		{
			errorProvider.SetError(tbNewName, string.Empty);

			if (tbNewName.Text.Length < 1)
			{
				errorProvider.SetError(tbNewName, LocalizationExtender.GetString("RenameGroupNameEmptyGroupname"));
				return;
			}
			if (tbNewName.Text.Length > Verety.SharedMeta.DirectoryServiceMeta.Enums.IntConstants.MaxGroupNameLength)
			{
				errorProvider.SetError(tbNewName, LocalizationExtender.GetString("RenameGroupNameTooLong"));
				return;
			}
			if (!mGroupNameRegex.IsMatch(tbNewName.Text))
			{
				errorProvider.SetError(tbNewName, LocalizationExtender.GetString(this, "RenameGroupNameSpecialCharacter"));
				return;
			}
			if (tbNewName.Text.Trim().ToLower() == Verety.SharedMeta.DirectoryServiceMeta.Enums.StringConstants.UnfiledGroupName.ToLower())
			{
				errorProvider.SetError(tbNewName, LocalizationExtender.GetString(this, "RenameGroupNameUnfiled"));
				return;
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			tbNewName.Text = tbNewName.Text.Trim();
			tbNewName_TextChanged(sender, e);
			if (errorProvider.GetError(tbNewName) != string.Empty)
			{
				return;
			}
			if (tbNewName.Text != mGroupName)
			{
				mContactListControl.DirectoryService.Contacts.RenameGroup(mGroupName, tbNewName.Text);
				mContactListControl.DirectoryService.Contacts.GetContactList();
			}
			Finalize();
			ParentModule.Host.Smartparts.Remove(this);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			Finalize();
			ParentModule.Host.Smartparts.Remove(this);
		}

		#endregion
	}
}
